﻿using System.Collections.Generic;
using DG.Tweening;
using Gp.Scripts.Core;
using QFramework;
using Sirenix.OdinInspector;
using TMPro;
using UnityEngine;
using UnityEngine.UI;



namespace Gp.Scripts.GUI {
    public class UnitLifeBarController : MonoBehaviour, IController {
        // public TMP_Text unitName;
        public HealthBarUI healthBar;
        public HealthBarUI moraleBar;

        [Title("文本")]
        [LabelText("生命值点数")] public TMP_Text healthPoint;
        [LabelText("士气点数")] public TMP_Text moralePoint;

        [LabelText("单位防御能力")] public TMP_Text defenceIntensity;
        [LabelText("单位行动点")] public TMP_Text actionPoint;
        [LabelText("单位防御点")] public TMP_Text reactionPoint;
        [SerializeField] private TMP_Text nameText;
        


        public Image actionBg;
        public Image reactionBg;
        public Color actionColor;
        public Color reactionColor;
        [LabelText("叠加颜色（随行动点数量叠加）")]
        public Color additiveColor;

        public CanvasGroup damagePredictCanvas;
        public TMP_Text damagePredictNumber;
        
        
        
        [Title("物体")]
        [LabelText("法力点")] public TMP_Text magicPoint;
        public GameObject magicPointParent;

        public Transform statusParent;
        public LifeBarStatusIcon statusIconPrefab;

        public Transform armorParent;
        public UnitArmorIcon armorIconPrefab;
        public UnitArmorIcon shieldArmorIconPrefab;
        private List<UnitArmorIcon> _armorIcons;


        public GameObject window;

        // private Animator _animator;

        private Dictionary<string, LifeBarStatusIcon> _dic;
        private static readonly int Expend = Animator.StringToHash("Expend");




        private void Start() {
            // _animator = GetComponent<Animator>();
            _dic = new Dictionary<string, LifeBarStatusIcon>();
            _armorIcons = new List<UnitArmorIcon>();
            // _animator.SetBool(Expend, true);

            MainArchitecture.Interface.RegisterEvent<Event_Unit.Highlight>(OnUnitHighlight)
                .UnRegisterWhenGameObjectDestroyed(gameObject);
            MainArchitecture.Interface.RegisterEvent<Event_Unit.DeHighlight>(OnUnitDeHighlight)
                .UnRegisterWhenGameObjectDestroyed(gameObject);

            window.Hide();
        }

        public void Render(BaseUnit unit) {
            window.Show();
            // 显示生命值 
            var curHealth = unit.CurHealth.Value;
            var maxHealth = unit.MaxHealth.Value;
            healthPoint.text = unit.ComCost[CostField.Health].ToString();
            healthBar.SetValue(curHealth, maxHealth);

            nameText.text = unit.UnitName;

            // // 更新护甲
            // foreach (var info in unit.EquipHolder.ArmorList) {
            //     var prefab = info.IsShieldFragment ? shieldArmorIconPrefab : armorIconPrefab;
            //
            //     var obj = prefab.InstantiateWithParent(armorParent, false);
            //     obj.Init(info, this);
            //     _armorIcons.Add(obj);
            // }
        }

        
        


        private void OnUnitDeHighlight(Event_Unit.DeHighlight eventUnitDeHighlight) {
            window.Hide();
        }

        private void OnUnitHighlight(Event_Unit.Highlight args) {
            Render(args.Unit);
        }


        public void FreshPredictUI() {
            // TryShowPredictUI();
        }
        
        
        // private void TryShowPredictUI() {
        //     var system = CellGridSystem.Instance;
        //     if (Unit.PlayerNumber == system.PlaceHolder.playerLeft.PlayerNumber) return;
        //
        //     var state = system.CellGridModel.CellGridState;
        //     if (state is not CellGridStateUnitPlay us || us.CurAbility == null) return;
        //     if (us.CurAbility.FirstOrDefault(x => x is SkillEntity se && se.PreCalculateResult(Unit) != null) is not SkillEntity ab) return;
        //     
        //     
        //     var res = ab.PreCalculateResult(Unit);
        //     if (res == null) return;
        //     // 护甲值闪烁
        //     foreach (var icon in _armorIcons) {
        //         if (res.ArmorDamageInfos.Any(x => x.ArmorFragment == icon.Info)) {
        //             icon.Blink();
        //         }
        //         else icon.UnBlink();
        //     }
        //     
        //     
        //     // 生命条闪烁
        //     healthBar.Blink(res.HealthDamageMake);
        //     moraleBar.Blink(res.MoraleDamage);
        //
        //     damagePredictCanvas.DOFade(1, 0);
        //     damagePredictNumber.text = res.ActualDamageMake.ToString();
        // }


        private void HidePredictUI() {
            foreach (var icon in _armorIcons) {
                icon.UnBlink();
            }
            healthBar.UnBlink();
            moraleBar.UnBlink();
            damagePredictCanvas.DOFade(0, 0f);
        }
        
        

        private void UpdateStatusUI(object sender, BuffChangeEvent args) {
            // 处理新增buff
            if (args.IsBuffAdd) {
                if (this._dic.ContainsKey(args.Buff.BuffId)) {
                    return;
                }


                var obj = statusIconPrefab.InstantiateWithParent(statusParent);
                obj.Render(args.Buff);
                _dic[args.Buff.BuffId] = obj;
            }
            else if (_dic.TryGetValue(args.Buff.BuffId, out var icon)) {
                _dic.Remove(args.Buff.BuffId);
                Destroy(icon.gameObject);
            }
        }



        public IArchitecture GetArchitecture() {
            return MainArchitecture.Interface;
        }
    }
}